package com.skyward.android.androiddemo2.view;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.skyward.android.androiddemo2.R;

/**
 * Created by skyward on 2017/3/15.
 * email:
 */

public class ViewPagerIndicator extends HorizontalScrollView implements ViewPager.OnPageChangeListener, View.OnClickListener {
    private ViewPager viewpager;
    private LinearLayout mLinearLayout;


    public ViewPagerIndicator(Context context) {
        super(context);
    }

    public ViewPagerIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ViewPagerIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setViewPager(ViewPager viewPager){
        this.viewpager = viewPager;
        viewPager.setOnPageChangeListener(this);

        mLinearLayout = new LinearLayout(getContext());
        mLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
        this.addView(mLinearLayout);

        for (int i = 0; i < viewPager.getAdapter().getCount(); i++) {
          View  mView = View.inflate(getContext(), R.layout.indicator,null);
            mView.setOnClickListener(this);
            TextView indicator_title = (TextView) mView.findViewById(R.id.indicator_title);
            TextView indicator_line = (TextView) mView.findViewById(R.id.indicator_line);
            indicator_title.setText(viewPager.getAdapter().getPageTitle(i));

            if(i == 0){
                indicator_line.setVisibility(VISIBLE);
                indicator_title.setTextColor(Color.RED);
            }else {
                indicator_line.setVisibility(INVISIBLE);
                indicator_title.setTextColor(Color.BLACK);
            }

            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(10,5,10,5);
            mLinearLayout.addView(mView,layoutParams);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        for (int i = 0; i < mLinearLayout.getChildCount(); i++) {
            View view = mLinearLayout.getChildAt(i);

            TextView indicator_title = (TextView) view.findViewById(R.id.indicator_title);
            TextView indicator_line = (TextView) view.findViewById(R.id.indicator_line);

            if(i == position){
                this.scrollTo((int)mLinearLayout.getChildAt(i).getX() - mLinearLayout.getChildAt(i).getWidth(),0);
                indicator_title.setTextColor(Color.RED);
                indicator_line.setVisibility(VISIBLE);
            }else {
                indicator_title.setTextColor(Color.BLACK);
                indicator_line.setVisibility(INVISIBLE);
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View v) {
        for (int i = 0; i < mLinearLayout.getChildCount(); i++) {
            if(v == mLinearLayout.getChildAt(i)){
                viewpager.setCurrentItem(i);
            }
        }
    }
}
